package com.smiletrain.activities;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.smiletrain.R;
import com.smiletrain.activities.base.AbstractActivity;
import com.smiletrain.base.Paciente;
import com.smiletrain.util.AdminCasos;
import com.smiletrain.util.Constantes;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CasoActivity extends AbstractActivity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.caso_view);
		super.setFootLayout();
		
		AlertDialog.Builder builder = new AlertDialog.Builder(CasoActivity.this);
		builder.setMessage("¿Esta seguro de eliminar el caso?")
			.setCancelable(false)
			.setPositiveButton("Si", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					AdminCasos.instance.eliminarCaso(caso.getId());
					Intent intent = new Intent(CasoActivity.this, MisCasosActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					overridePendingTransition(R.anim.in_anim, R.anim.back_anim);
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
		
		alert = builder.create();
		
		Paciente pa = caso.getPaciente(); 
		
		TextView paciente = (TextView) findViewById(R.id.txtPaciente);
		paciente.setText(pa != null ? pa.getNombres() : "");
		
		TextView nombre = (TextView) findViewById(R.id.txtNombre);
		nombre.setText(pa != null ? pa.getNombres() : "");
		
		TextView apellidos = (TextView) findViewById(R.id.txtApellidos);
		apellidos.setText(pa != null ? pa.getApellidos() : "");
		
		TextView genero = (TextView) findViewById(R.id.txtGenero);
		genero.setText(pa != null ? pa.isGenero() ? "Masculino" : "Femenino" : "");
		
		TextView fecha = (TextView) findViewById(R.id.txtFecha);
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		fecha.setText(sdf.format(new Date(caso.getTime())));
		
		TextView intervencion = (TextView) findViewById(R.id.txtIntervencion);
		String inter = caso.getIntervencionNumerica();
		String rinter = "";
		
		for(String key : Constantes.TIPO_INTERVENCION.keySet()) {
			if(inter.contains(key)) {
				rinter += Constantes.TIPO_INTERVENCION.get(key) + " / ";
			}
		}
		
		if(rinter.length() >= 4)
			rinter = rinter.substring(0, rinter.length() - 3);
		
		intervencion.setText(rinter);
		
		if(!caso.getPreFotos().getPathImage1().equals("")){
			ImageView pre1 = (ImageView) findViewById(R.id.imgPre1);
			MuestraFoto(caso.getPreFotos().getPathImage1(), pre1);
			pre1.setAdjustViewBounds(true);
			pre1.setMaxHeight(290);
		}
		
		if(!caso.getPreFotos().getPathImage2().equals("")){
			ImageView pre2 = (ImageView) findViewById(R.id.imgPre2);
			MuestraFoto(caso.getPreFotos().getPathImage2(), pre2);
			pre2.setAdjustViewBounds(true);
			pre2.setMaxHeight(290);
		}
		
		if(!caso.getPostFotos().getPathImage1().equals("")){
			ImageView post1 = (ImageView) findViewById(R.id.imgPost1);
			MuestraFoto(caso.getPostFotos().getPathImage1(), post1);
			post1.setAdjustViewBounds(true);
			post1.setMaxHeight(290);
		}
		
		if(!caso.getPostFotos().getPathImage2().equals("")){
			ImageView post2 = (ImageView) findViewById(R.id.imgPost2);
			MuestraFoto(caso.getPostFotos().getPathImage2(), post2);
			post2.setAdjustViewBounds(true);
			post2.setMaxHeight(290);
		}
		
		ImageView eliminar = (ImageView) findViewById(R.id.bEliminar);
        
        eliminar.setOnClickListener(new OnClickListener() {
        	
			@Override
			public void onClick(View v) {
				alert.show();
			}
			
		});
        
        ImageView continuar = (ImageView) findViewById(R.id.bContinuar);
        
        continuar.setOnClickListener(new OnClickListener() {
        	
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CasoActivity.this, TipoActivity.class);
				startActivity(intent);
				finish();
			}
			
		});
        
	}
	
	@Override
	public void updateCaso() {	
	}
	
	private void MuestraFoto(String arch, ImageView view) {
		try {
			BitmapFactory.Options op = new BitmapFactory.Options();
			op.inJustDecodeBounds = true;
			int scale = 1;
			FileInputStream fd = new FileInputStream(arch);

			BitmapFactory.decodeFileDescriptor(fd.getFD(), null, op);
			
			if (op.outHeight > Constantes.IMAGE_MAX_SIZE || op.outWidth > Constantes.IMAGE_MAX_SIZE) {
				double d = Math.pow(2, (int) Math.round(Math.log(Constantes.IMAGE_MAX_SIZE / (double) Math.max(op.outHeight, op.outWidth)) / Math.log(0.5)));
				scale = (int) d;
			}
			
			op = new BitmapFactory.Options();
			op.inSampleSize = scale;

			view.setImageBitmap(BitmapFactory.decodeFileDescriptor(fd.getFD(),null,op));
			
			fd.close();
			
		} catch(Exception e) {
			Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		}
	}
	
	private AlertDialog alert;
	
}
